const GZHConfig = require("./gzhConfig");
const { decodeEncrypt } = require("./tool/decode");
const { paramCheck } = require("./tool/dataCheck");
const { dispatch } = require("./action/dispatch");
function handleGZHMessage(params, httpInfo) {
    console.log("函数中参数 ", params);
    console.log("http请求  ", httpInfo.body);
    
    // params = {
    //     signature: "2aa6638786dfe5eedc6a1d86329d637cd5265719",
    //     timestamp: "1724596257",
    //     nonce: "178463834",
    //     openid: "oxWk-5vaskY0-jeEH3eVmUasQHxA",
    //     encrypt_type: "aes",
    //     msg_signature: "72abe4431c662eb46a350c412852133f3e543886",
    // };

    // httpInfo = {
    //     path: "/gongzhonghaoBack",
    //     httpMethod: "POST",
    //     headers: {
    //         accept: "*/*",
    //         "cache-control": "no-cache",
    //         "content-length": "578",
    //         "content-type": "text/xml",
    //         pragma: "no-cache",
    //         service: "wxCallBack",
    //         "sofa-rpcid": "0a4b4cf117245962582692131221",
    //         "sofa-traceid": "0a4b4cf117245962582692131221",
    //         sofa_head_method_name: "/wxback/gongzhonghaoBack",
    //         "user-agent": "Mozilla/4.0",
    //         "x-djy-uuid": "13d19b64-44ba-402a-b5dc-2bd9ffacd7b5",
    //         "x-faas-context-bindconfigs": '{"NEED_AUTH":"false","NEED_SIGN":"false"}',
    //         "x-faas-context-binddomain": "zfbapi.wangnangua.top",
    //         "x-faas-context-bindpath": "/wxback",
    //         "x-faas-runtime-call-context":
    //             '{"appId":"2021004124637907","traceId":"0a4b4cf117245962582692131221","rpcId":"0.1.2","requestId":"c31debaa-7f7c-41e0-9d1b-6f8e03a69270","eventType":"http"}',
    //         "x-faas-source-type": "httpCustom",
    //         "x-forwarded-for": "81.69.103.129",
    //         "x-forwarded-proto": "https",
    //         "x-real-ip": "81.69.103.129",
    //         "x-stream-request-id": "5a7ceb45-0dbf-42bb-ad00-73ca73492e5b",
    //         "x5-uuid": "13d19b64-44ba-402a-b5dc-2bd9ffacd7b5",
    //     },
    //     queryStringParameters: {
    //         signature: "2aa6638786dfe5eedc6a1d86329d637cd5265719",
    //         timestamp: "1724596257",
    //         nonce: "178463834",
    //         openid: "oxWk-5vaskY0-jeEH3eVmUasQHxA",
    //         encrypt_type: "aes",
    //         msg_signature: "72abe4431c662eb46a350c412852133f3e543886",
    //     },
    //     body:
    //         "<xml>\n" +
    //         "    <ToUserName><![CDATA[gh_277bf55051bb]]></ToUserName>\n" +
    //         "    <Encrypt><![CDATA[CAmYaZ8PUe2EXQEJ1x2Dm514sXv6FsyBYwzPILz5iH6890/QivQZSrOi/24mILSYu4r4Gn/jljsHkDlNd3RuUGhdtOxad9eElj5hJNmPkIX2RRCc5g9OzHNEFARbbEp7mHJ6Ni5OJ+Hwm/NZhj2BpfbOQoFaPhZxsCPs0XSrCUMdvumYnYVmkoJ/0k+0FAuScnO/jR3NBPhEe4MNlzH1b6b5eYDEOp6BYxXj2lUQrgi6ykBVxPXYRab240NT6l4YjFm7ZPWFlwPu2TooR2BqFJeyN6f7uu/E/swHLRuhhHPZ6N/nOXMRNV4s5teB6n1hSvgCXVJSqYvyM4gpZwx4XZCda6lvgXlQt3FjJYN4DmLpd/a14S8nAQlOFSoP33SiD1jqPWcE/jbr+2sm57kZYKIg+gBD4BjGu5Y1uxxBAwy3LqOKXM1VXkvbOmBjVmp7UFJHP8PwjqfVHMwTnMGnaA==]]></Encrypt>\n" +
    //         "</xml>\n",
    //     isBase64Encoded: false,
    // };

    // 判断 请求是不是 微信来的
    let paramOk = paramCheck(params, httpInfo);
    if (!paramOk.flag) {
        return;
    }
    // 解密
    let decode = decodeEncrypt(paramOk.encrypt) || {};
    console.log("🚀🚀🚀 ~ 解密 数据 🙉🙊🙈", decode);
    // 判断 appId是否一致, 这里不判断了
    // if (GZHConfig.appId !== decode.appId) {
    //     return;
    // }
    // 派发事件
    const result = dispatch(decode.content, decode.appId, params.nonce);
    console.log("最后返回的是 --", result);
    return result;
}

module.exports = {
    handleGZHMessage,
};
